//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.util;

import com.sun.management.HotSpotDiagnosticMXBean;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanServer;

public class JvmUtil {
    private static final Logger logger = Logger.getLogger(JvmUtil.class.getName());

    public JvmUtil() {
    }

    public static boolean dumpHeap(String fileName, boolean live) {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        try {
            logger.fine("Taking heap dump..");
            HotSpotDiagnosticMXBean hotspotMBean = (HotSpotDiagnosticMXBean)ManagementFactory.newPlatformMXBeanProxy(server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
            hotspotMBean.dumpHeap(fileName, live);
            logger.fine("Heap dump done");
            return true;
        } catch (Throwable var4) {
            logger.log(Level.WARNING, "Error: " + var4, var4);
            return false;
        }
    }

    public static boolean dumpJmapHisto(String fileName) {
        String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
        int pi = nameOfRunningVM.indexOf(64);
        String pid = nameOfRunningVM.substring(0, pi);
        String command = "jmap -histo " + pid;
        Process p = null;

        try {
            logger.log(Level.INFO, "Command executing : {0}", command);
            p = Runtime.getRuntime().exec(command);
            dumpProcessOutputToFile(p, fileName);
            logger.log(Level.INFO, "Command executed : {0}", command);
            return true;
        } catch (IOException var7) {
            logger.log(Level.WARNING, "Error: " + var7, var7);
            return false;
        }
    }

    public static boolean dumpJmapHistoToLog() {
        String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
        int pi = nameOfRunningVM.indexOf(64);
        String pid = nameOfRunningVM.substring(0, pi);
        String command = "jmap -histo " + pid;
        Process p = null;

        try {
            logger.log(Level.INFO, "Command executing : {0}", command);
            p = Runtime.getRuntime().exec(command);
            dumpProcessOutputToLog(p);
            logger.log(Level.INFO, "Command executed : {0}", command);
            return true;
        } catch (IOException var6) {
            logger.log(Level.WARNING, "Error: " + var6, var6);
            return false;
        }
    }

    public static boolean threadDump(String fileName) {
        ThreadInfo[] threadsInfo = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
        dumpToFile(threadsInfo, fileName);
        return true;
    }

    public static boolean threadDumpToLog() {
        ThreadInfo[] threadsInfo = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
        dumpToLog(threadsInfo);
        return true;
    }

    public static boolean dumpJStack(String fileName) {
        String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
        int pi = nameOfRunningVM.indexOf(64);
        String pid = nameOfRunningVM.substring(0, pi);
        String command = "jstack -l " + pid;
        Process p = null;

        try {
            logger.log(Level.INFO, "Command executing : {0}", command);
            p = Runtime.getRuntime().exec(command);
            dumpProcessOutputToFile(p, fileName);
            logger.log(Level.INFO, "Command executed : {0}", command);
            return true;
        } catch (IOException var7) {
            logger.log(Level.WARNING, "Error: " + var7, var7);
            return false;
        }
    }

    public static boolean dumpJStackToLog() {
        String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
        int pi = nameOfRunningVM.indexOf(64);
        String pid = nameOfRunningVM.substring(0, pi);
        String command = "jstack -l " + pid;
        Process p = null;

        try {
            logger.log(Level.INFO, "Command executing : {0}", command);
            p = Runtime.getRuntime().exec(command);
            dumpProcessOutputToLog(p);
            logger.log(Level.INFO, "Command executed : {0}", command);
            return true;
        } catch (IOException var6) {
            logger.log(Level.WARNING, "Error: " + var6, var6);
            return false;
        }
    }

    public static void dumpProcessOutputToFile(Process p, String fileName) {
        BufferedWriter writer = null;
        BufferedReader reader = null;

        try {
            logger.log(Level.INFO, "Start of ProcessOutput to file {0}", fileName);
            String line = null;
            reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            writer = new BufferedWriter(new FileWriter(fileName));

            while((line = reader.readLine()) != null) {
                writer.write(line);
                writer.write("\r\n");
            }

            writer.flush();
            logger.log(Level.INFO, "End of ProcessOutput to file {0}", fileName);
        } catch (Exception var17) {
            logger.log(Level.WARNING, "Exception: " + var17, var17);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception var16) {
                logger.log(Level.WARNING, "Exception: " + var16, var16);
            }

            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (Exception var15) {
                logger.log(Level.WARNING, "Exception: " + var15, var15);
            }

            if (p != null) {
                p.destroy();
            }

        }

    }

    public static void dumpProcessOutputToLog(Process p) {
        BufferedReader reader = null;

        try {
            logger.log(Level.INFO, "Start of ProcessOutput to log ");
            String line = null;
            reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

            while((line = reader.readLine()) != null) {
                logger.info(line);
            }

            logger.log(Level.INFO, "End of ProcessOutput to log");
        } catch (Exception var11) {
            logger.log(Level.WARNING, "Exception: " + var11, var11);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception var10) {
                logger.log(Level.WARNING, "Exception: " + var10, var10);
            }

            if (p != null) {
                p.destroy();
            }

        }

    }

    public static void dumpToFile(ThreadInfo[] threadsInfo, String fileName) {
        File file = null;
        BufferedWriter writer = null;

        try {
            logger.log(Level.INFO, "Start of Thread dump to file {0}", fileName);
            file = new File(fileName);
            writer = new BufferedWriter(new FileWriter(file));
            ThreadInfo[] var7 = threadsInfo;
            int var6 = threadsInfo.length;

            for(int var5 = 0; var5 < var6; ++var5) {
                ThreadInfo t = var7[var5];
                writer.write(t.toString());
            }

            writer.flush();
            logger.log(Level.INFO, "End of Thread dump to file {0}", fileName);
        } catch (IOException var16) {
            logger.log(Level.WARNING, "IOException: " + var16, var16);
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (Exception var15) {
                logger.log(Level.WARNING, "Exception: " + var15, var15);
            }

        }

    }

    public static void dumpToLog(ThreadInfo[] threadsInfo) {
        logger.info("Start of Thread dump to log");
        ThreadInfo[] var4 = threadsInfo;
        int var3 = threadsInfo.length;

        for(int var2 = 0; var2 < var3; ++var2) {
            ThreadInfo t = var4[var2];
            logger.info(t.toString());
        }

        logger.info("End of Thread dump to log");
    }
}
